import pygame, random, sys
from pygame.locals import *

import classes
import functions
import player
import earthcity
import mastercontrol


WINDOWWIDTH = 640
WINDOWHEIGHT = 480
SCALEGFX = 2
TEXTCOLOR = (255, 255, 255)
BACKGROUNDCOLOR = (0, 0, 0)
FPS = 40

def terminate():
    pygame.quit()
    sys.exit()


# set up pygame, the window, and the mouse cursor
pygame.init()
mainClock = pygame.time.Clock()
windowSurface = pygame.display.set_mode((WINDOWWIDTH, WINDOWHEIGHT), 0, 32)
pygame.display.set_caption('pyDeuteros Very early build')
pygame.mouse.set_visible(False)
pygame.event.set_grab(False) 
timepassed = 0

# Info
locationInfo = classes.LocationInfo('locations.dat')
iteminfo = classes.ItemInfo('items.dat', SCALEGFX)
dialogbox = classes.dialogBox(windowSurface, SCALEGFX)

# Setup sound
mouseOverSound = pygame.mixer.Sound('sound/mouseover.ogg')
mouseClickSound = pygame.mixer.Sound('sound/mouseclick.ogg')

# set up mouse
MouseIconDict = functions.scaled_image_dict('mouse', 'mouse', 'mouse', 'gfx/Pointer.png', SCALEGFX, WINDOWWIDTH/2, WINDOWHEIGHT/2)
mouseOver = classes.mouseOver(SCALEGFX)

# Setup the Player class
player = player.player()

# Init Screens
mastercontrolscreen = mastercontrol.mastercontrol(windowSurface, SCALEGFX)
earthcity = earthcity.earthcity(windowSurface, SCALEGFX, locationInfo, player.getProdItems())

currentscreen = 'earthcity'
currentsubscreen = 'main'
location = 'earthcity'
solsystem, solsystemname = locationInfo.GetSolSystemName(location)
defaultmusic = 'sound/earthcity.ogg'
currentmusic = classes.which_music(defaultmusic)
requestedmusic = False


# set up messaging
toplefttext = classes.toplefttext(windowSurface, SCALEGFX)
solsystemtext = classes.solsystemtext(windowSurface, SCALEGFX)
theTicker = classes.theTicker(windowSurface, SCALEGFX)
year = False
tick = False

interactiveIcons = []

while True:

	topleftmessage = False
	del interactiveIcons [:]

	for event in pygame.event.get():
		if event.type == QUIT:
			pygame.quit()
			sys.exit()
		if event.type == MOUSEMOTION:
	                MouseIconDict['rect'].move_ip(event.pos[0] - MouseIconDict['rect'].centerx, event.pos[1] - MouseIconDict['rect'].centery)
		if event.type == MOUSEBUTTONUP:
			if event.button == 3:
				currentscreen, currentsubscreen = player.rightclickgoeswhere()
				dialogbox.unsetDialog()
			elif (mouseOverIcon):
				mouseClickSound.play()
				dialogbox.unsetDialog()
				if (iconType == 'subscreen'):
					currentsubscreen = iconName
				elif (iconType == 'screen'):
					currentsubscreen = False
					currentscreen = iconName
				elif (iconType == 'tick'):
					year, tick =  player.dotick(player, iteminfo, earthcity)
				elif (iconType == 'prodTeamToBay'):
					if (currentscreen == 'earthcity'):
						player.moveProdShuttleBay('earthcity')
				elif (iconType == 'engtoprod'):
					if (currentscreen == 'earthcity'):
						player.moveProdtoProd('earthcity', iconName)
				elif (iconType == 'addrig'):
					if (currentscreen == 'earthcity'):
						earthcity.AddRig()
				elif (iconType == 'switchstore'):
					if (currentscreen == 'earthcity'):
						earthcity.toggleStorage()
				elif (iconType == 'build'):
					if (currentscreen == 'earthcity'):
						earthcity.addProduction(iconName, iteminfo, player, dialogbox)
				elif (iconType == 'storeselect'):
					if (currentscreen == 'earthcity'):
						earthcity.SetStoreSelectedItem(iconName)
				elif (iconType == 'research'):
					earthcity.SetResearchSelectedItem(iconName)
				elif (iconType == 'startresearch'):
					earthcity.SetResearchSelectedItem(iconName)
					earthcity.AddResearch(iconName, iteminfo, player, dialogbox)
				elif (iconType == 'newshuttle'):
					if (currentscreen == 'earthcity'):
						earthcity.NewShuttle(dialogbox)
				elif (iconType == 'remshuttle'):
					if (currentscreen == 'earthcity'):
						earthcity.RemoveShuttle()
				elif (iconType == 'shuttleselect'):
					if (currentscreen == 'earthcity'):
						earthcity.ManageShuttle(iconName)
				
						
		if event.type == MOUSEBUTTONDOWN:
			if (iconType == 'training'):
				mouseClickSound.play()
				player.add_rem_training(iconName)
		if event.type == KEYUP:
	                if event.key == K_ESCAPE:
                        	terminate()

	mouse1, mouse2, mouse3 = pygame.mouse.get_pressed()
	if (mouse1 == 1):
		if (iconType == 'training'):
			player.add_rem_training(iconName)
		elif (iconType == 'tick'):
			year, tick =  player.dotick(player, iteminfo, earthcity)
	
	windowSurface.fill(BACKGROUNDCOLOR)
	mastercontrolscreen.display(currentscreen, timepassed, player)
	interactiveIcons = mastercontrolscreen.myicons()

	if (currentscreen == 'earthcity'):
		requestedmusic = earthcity.whichscreen(currentsubscreen, earthcity, player, iteminfo, timepassed)
		interactiveIcons.extend(earthcity.myicons())
	elif (currentscreen == 'orbital'):
		currentscreen = 'orbital'
	elif (currentscreen == 'ios'):
		currentscreen = 'ios'
	elif (currentscreen == 'scc'):
		currentscreen = 'scc'
	elif (currentscreen == 'mainmenu'):
		requestedmusic = False
		currentmusic.stop()
		mastercontrolscreen.mainmenu()
	else:
		mastercontrolscreen.displaysolarcontrol(solsystem)

	if (dialogbox.getIcon()):
		del interactiveIcons [:]
		interactiveIcons.append(dialogbox.getIcon())
	mouseOverIcon, mouseOverIconSelection, iconName, iconType, topleftmessage = mouseOver.mouseOverDetect(MouseIconDict['rect'], mouseOverSound, interactiveIcons, iteminfo, windowSurface)
	dialogbox.showDialog()
	currentmusic.update(requestedmusic)
	windowSurface.blit(MouseIconDict['surface'], MouseIconDict['rect'])
	toplefttext.update(topleftmessage)
	solsystemtext.update(solsystemname)
	theTicker.update(year, tick)
	pygame.display.update()
	timepassed = mainClock.tick_busy_loop(FPS)


